package com.example.fill;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.fill.databinding.ActivityMainLandscapeBinding;
import com.example.fill.databinding.ActivityMainPadBinding;
import com.example.fill.databinding.ActivityMainPortraitBinding;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ActivityMainLandscapeBinding landscapeBinding;
    ActivityMainPortraitBinding portraitBinding;
    ActivityMainPadBinding padBinding;


    private AppBarLayout appbarLayout;
    private Toolbar toolBar;
    private TabLayout tabLayout;
    private RecyclerView listView;

    private NavigationView navigationView;


    private boolean isTable;
    private boolean mOrientation;

    private int column = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isTable = isTableDevice();
        if (isTable) {
            if (isTableWindow()) {
                int orientation = getResources().getConfiguration().orientation;
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mOrientation = false;
                    setContentView(R.layout.activity_main_landscape);
                } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                    mOrientation = true;
                    setContentView(R.layout.activity_main_portrait);
                    column = 2;
                }
            } else {
                setContentView(R.layout.activity_main_pad);
                column = 4;
            }
        } else {
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mOrientation = false;
                setContentView(R.layout.activity_main_landscape);
                column = 2;
            } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                mOrientation = true;
                setContentView(R.layout.activity_main_portrait);
            }
        }
        findView();
        initView();
    }

    private void findView() {
        appbarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
        toolBar = (Toolbar) findViewById(R.id.tool_bar);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        listView = (RecyclerView) findViewById(R.id.list_view);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);
    }

    private void initView() {
        if (mOrientation) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText("食品");
            tabLayout.addTab(tab);
            TabLayout.Tab tab1 = tabLayout.newTab();
            tab1.setText("酒水");
            tabLayout.addTab(tab1);
        } else {
            navigationView.inflateMenu(R.menu.main_menu);
        }

        Adapter adapter = new Adapter();
        listView.setAdapter(adapter);
        if (column > 1) {
            listView.setLayoutManager(new GridLayoutManager(this, column));
        } else {
            listView.setLayoutManager(new LinearLayoutManager(this));
        }
        adapter.submitList(getData());
    }

    private boolean isTableDevice() {
        return SystemPropertiesProxy.get(this, "ro.build.characteristics").contains("tablet");
    }

    /**
     * 动态判断是否平板窗口
     * 在平板设备上，也可能返回false。如分屏模式下
     * 如想判断物理设备是不是平板，请使用 isTabletDevice
     *
     * @return true:平板,false:手机
     */
    private boolean isTableWindow() {
        return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    private List<Bean> getData() {
        ArrayList<Bean> beans = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            beans.add(new Bean(i + 1, "Test " + i, "content " + i));
        }
        return beans;
    }
}